const EventEmitter = require('events');

class CustomEvent extends EventEmitter{

}

const ce = new CustomEvent();

ce.on('test', () => {
    console.log('this is event test.');
});

function fn2(){
    console.log('fn2');
}

ce.on('test', fn2);
// 会同时触发上面的两个 ‘test’ 事件
setInterval(() => {
    ce.emit('test');
}, 500);

ce.on('error', err =>{
    console.log(err);
});
ce.emit('error', new Error('oops'), Date.now());

//只会触发一次
ce.once('onceTest', () =>{
    console.log('onceTest');
});

setInterval(() => {
    ce.emit('onceTest');
}, 500);

// 过 2秒后移除 fn2
setTimeout(() => {
    ce.removeListener('test', fn2); 
}, 2000);

